package cn.kgc.javaweb.servlet;

import cn.kgc.javaweb.entity.Student;
import cn.kgc.javaweb.service.StudentService;
import cn.kgc.javaweb.service.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/6/25
 */
@WebServlet("/student")
public class StudentServlet extends BaseServlet {

    private StudentService studentService =  new StudentServiceImpl();

    //查询所有学生信息
    protected void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

      List<Student> students =  studentService.findAll();

      req.getSession().setAttribute("students", students);
      resp.sendRedirect(req.getContextPath()+"/studentList.jsp");


    }

    protected void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id = req.getParameter("id");

        Student student =  studentService.findById(Integer.parseInt(id));
        req.getSession().setAttribute("student", student);

        resp.sendRedirect(req.getContextPath()+"/updateStudent.jsp");

    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

        //获取修改参数
        String id = req.getParameter("id");
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");
        String stuAge = req.getParameter("stuAge");
        String stuBirth = req.getParameter("stuBirth");


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birth = simpleDateFormat.parse(stuBirth);

        Student student = Student.builder().id(Integer.parseInt(id)).stuNo(stuNo).stuName(stuName).stuAge(Integer.parseInt(stuAge)).stuBirth(birth).build();

         Boolean b =  studentService.updateStudent(student);

         if(b){ // 修改成功 跳转主页面

                resp.sendRedirect(req.getContextPath()+"/student?method=toList");

         }else{

             req.setAttribute("info", "修改失败");
             req.getRequestDispatcher("updateStudent.jsp").forward(req, resp);

         }

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String id = req.getParameter("id");

        Boolean b =   studentService.deleteById(Integer.parseInt(id));

        if(!b){
                req.getSession().setAttribute("deleteInfo","删除失败");
        }
        resp.sendRedirect(req.getContextPath()+"/student?method=toList");
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

        //获取修改参数
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");
        String stuAge = req.getParameter("stuAge");
        String stuBirth = req.getParameter("stuBirth");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birth = simpleDateFormat.parse(stuBirth);

        Student student = Student.builder().stuNo(stuNo).stuName(stuName).stuAge(Integer.parseInt(stuAge)).stuBirth(birth).build();

        Boolean b =   studentService.addStudent(student);

        if(b){
            resp.sendRedirect(req.getContextPath()+"/student?method=toList");
        }else{
            req.setAttribute("addInfo", "添加失败");
            req.getRequestDispatcher("addStudent.jsp").forward(req, resp);
        }
    }


}
